
#!/usr/bin/env bash

root="$(git rev-parse --show-toplevel)"

# get the list of changed files
staged_files="$(git diff --cached --name-only)"

echo "Running php-cs-fixer on edited files"

for staged in ${staged_files}; do
    # work only with existing files
    if [[ -f ${staged} && ${staged} == *.php ]]; then
        # use php-cs-fixer and get flag of correction
        "${root}/bin/php-cs-fixer" -q fix "${staged}"

        # if php-cs-fixer fix works, it returns 0
        if [[ $? -eq 0 ]]; then
            git add "${staged}" # execute git add directly
        fi
    fi
done

echo "Running php-doc-checker"

"${root}/bin/php-doc-check" src plugins components

# Only commit if there wasn't an error
exit $?
